//    This file is part of Troops & Flags DS.
//
//    Troops & Flags DS is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    Troops & Flags DS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Modabu; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

//    Copyright 2007, Daniel McIntyre, Priyesh Dixit, Jonathan Annas, Robert Hammond, Suhyung Cho

// Includes
#include <PA9.h>       // Include for PA_Lib

// PAGfx Includes
#include "gfx/all_gfx.c"
#include "gfx/all_gfx.h"
// Audio includes
#include "music.h"

////////////////////////////////////////////////
//Notes:
//Screen size: 255,191~
//Play sound:
//PA_PlaySimpleSound(0, saberoffsound);
//PA_PlaySoundRepeat(0, saberoffsound);

//Load sprite from PAgfx:
//PA_LoadSpritePal(0, // Bottom Screen
//					0, // Palette number
//					(void*)sprite0_Pal);	// Palette name
//PA_CreateSprite(0, 0,(void*)paddle_Sprite, OBJ_SIZE_64X64,1, 0, 50, 50);
//screenID, objID, data, shape, size, color mode, palette, x,y

//Waiting.. 60 frames ~= 1 secon

//coordinates: down = +1, right= +1
////////////////////////////////////////////////


enum GAME_STATE {MYSPLASH, MAIN_MENU, GAME, PAUSE, GAME_OVER, YOU_WIN};

void initMySplash()
{
    
	PA_LoadSpritePal(1, // Screen
					0, // Palette number
					(void*)prilogo_Pal);	// Palette name
					
	PA_CreateSprite(1, 0,(void*)prilogo_Sprite, OBJ_SIZE_64X64,1, 0, 90, 70);
    
	PA_OutputText(0, 5,5, "Troops & Flags DS");
	PA_OutputText(0, 8,6, "using PALib");	
}

void destroyMySplash()
{
	PA_DeleteSprite(1,0);
	PA_ClearTextBg(0);
}

void initMainMenu()
{
	PA_EasyBgLoad(0, 3, bottomBG);
	PA_EasyBgLoad(1, 3, topBG);
}

void destroyMainMenu()
{
	PA_DeleteBg(0, 3);
	PA_DeleteBg(1, 3);
}

void initGameScreen()
{
    /*
	PA_LoadSpritePal(0, // Screen
					0, // Palette number
					(void*)paddle_Pal);	// Palette name
					
	PA_CreateSprite(0, 0,(void*)paddle_Sprite, OBJ_SIZE_64X64,1, 0, 100, 165);
	//screenID, objID, data, shape, size, color mode, palette, x,y
	
	PA_DualLoadSpritePal(
					1, // Palette number
					(void*)ball_Pal);	// Palette name
					
	PA_DualCreateSprite(1,(void*)ball_Sprite, OBJ_SIZE_32X32,1, 1, 124, 83);
	
	PA_EasyBgLoad(0, 3, gamebottomBG);
	PA_EasyBgLoad(1, 3, gametopBG);	
    */
}

void destroyGameScreen()
{
    /*
	PA_DeleteSprite(0, 0);
	PA_DualDeleteSprite(1);
	
	PA_DeleteBg(0, 3);
	PA_DeleteBg(1, 3);
    */
}

void initGameOver()
{
	//PA_EasyBgLoad(0, 3, gameoverbottomBG);
	//PA_EasyBgLoad(1, 3, gameovertopBG);
}

void destroyGameOver()
{
	//PA_DeleteBg(0, 3);
	//PA_DeleteBg(1, 3);
}

void initYouWin()
{
	//PA_EasyBgLoad(0, 3, winbottomBG);
	//PA_EasyBgLoad(1, 3, wintopBG);
}

void destroyYouWin()
{
	//PA_DeleteBg(0, 3);
	//PA_DeleteBg(1, 3);
}

//Splash screen wait constant
const s16 SPLASH_WAIT_FRAMES = 240;

//Stops sound if channel is busy
void StopChannelIfBusy(u8 channel)
{
	if(PA_SoundChannelIsBusy(channel))
	{
		PA_StopSound(channel);
	}
}


void initializeGame()
{
	//initialize game
}

int main()
{
	GAME_STATE gameState;
	gameState = MYSPLASH;
	
	PA_Init();    // Initializes PA_Lib
	PA_InitVBL(); // Initializes a standard VBL
	PA_InitText(0,0);	
	PA_InitText(1,0);
	
	PA_InitSound();
	
	PA_SetScreenSpace(0);
	
	// Infinite loop to keep the program running
	while (1)
	{
		PA_ClearTextBg(0);
		PA_ClearTextBg(1);
		
		if (gameState == MYSPLASH)
		{
			//show my splash
			initMySplash();
			s16 time = SPLASH_WAIT_FRAMES;
			while (time)
			{
				time--;		
				PA_WaitForVBL();
			}
			destroyMySplash();
			gameState = MAIN_MENU;
		}
		else if (gameState == MAIN_MENU)
		{
			initMainMenu();
			//PA_PlaySimpleSound(3, welcomesound);
			while(!Pad.Newpress.Start && !Stylus.Newpress)
			{
				PA_WaitForVBL();
			}
			destroyMainMenu();
			
			//initializeGame(&gameInfo);
			//initGameScreen();
			//gameState = GAME;
		}
		else if (gameState == GAME_OVER)
		{
		}
		//goal reached.. now in YOUWIN state
		else if (gameState == YOU_WIN)
		{
		}
		else if (gameState == GAME || gameState == PAUSE)
		{
			//if paused display "Paused"
			if (gameState == PAUSE)
			{
                //change text color to black
				PA_SetTextCol(0, 0, 0, 0);
				PA_OutputText(0, 10, 6, "Paused");
				//check for unpause
				if (Pad.Newpress.Start)
				{
                    //change text color back to white
					PA_SetTextCol(0, 31, 31, 31);
					gameState = GAME;
				}
			}
			else//only do the rest if NOT paused
			{
				//background music
				if (PA_SoundChannelIsBusy(4) == false)
				{
					PA_PlaySimpleSound(4, music);
				}
								
				//check microphone for slowing down ball
				//u8 volume = PA_MicGetVol();
                				//check for pause
				if (Pad.Newpress.Start)
				{
					gameState = PAUSE;
					StopChannelIfBusy(0);
					StopChannelIfBusy(1);
					StopChannelIfBusy(2);
					StopChannelIfBusy(3);
					StopChannelIfBusy(4);
				}
			}
		}
		
		PA_WaitForVBL();
	}
	destroyGameScreen();
	return 0;
}
